<?php
/**
 * when the params eq. &not_as_menu will hiddened in the menu.
 * 当params 等于&not_as_menu 时, 将不在菜单里显示. 只作为权限用.
 */
class Menu {
	var $path = array ();

	function editForm($menus, $curMenu = array ()) {
		$str = '<ul>';
		foreach ($menus as $key => $menuItem) {
			$this->path[] = $key;
			$temMenu = $curMenu[$key];
			$mkey = implode('][', $this->path);
			if ($temMenu['name'] == $menuItem['name']) {
				$checked = ' checked="true"';
			} else {
				$checked = '';

			}

			$mid = 'p_menu_' . implode('_', $this->path);
			$str .= '<li><input ' . $checked . ' type="checkbox" onclick="chkClick(this)" name="p_menu[' . $mkey . ']" id="' . $mid . '" value="[' . $mkey . ']"><label for="' . $mid . '">' . $menuItem['name'] . '</label>';
			if (isset ($menuItem['items']) && is_array($menuItem['items']) && count($menuItem['items'])) {
				$this->path[] = "items";
				$str .= $this->editForm($menuItem['items'], $temMenu['items']);
				array_pop($this->path);
			}
			$str .= '</li>';
			array_pop($this->path);

		}
		$str .= '</ul>';
		return $str;
	}

	function disMenu($menus, $class = "tree") {
		if ($class != '') {
			$str = '<ul class="' . $class . '" id="adminMenu">';
		} else {
			$str = '<ul>';
		}
		$i = 1;
		foreach ($menus as $menuItem) {
			if(!empty($menuItem['params']) && ($menuItem['params'] == '&not_as_menu')) {
				continue;
			}
			$str .= '<li';
			if (isset ($menuItem['items']) && is_array($menuItem['items']) && count($menuItem['items'])) {
				if (count($menus) == $i) {
					$str .= ' class="Root last" ';
				} else {
					$str .= ' class="Root" ';
				}

				$str .= '>';
				$str .= '<a><strong>';
				$str .= '<font style="font-size: 12px;">'.$menuItem['name'].'</font>';
				$str .= '</strong></a>';
				$str .= $this->disMenu($menuItem['items'], '');
			} else {
				$str .= '><a href="sys.php?model=' . $menuItem['model'] . '&action=' . $menuItem['action'] . $menuItem['params'] . '"><font style="font-size: 12px;">' . $menuItem['name'] . '</font></a>';
			}
			$str .= '</li>';
			$i++;

		}
		$str .= '</ul>';
		return $str;
	}

	function deMenu($menus, $all) {

		$newMenus = array ();
		foreach ($menus as $key => $menuItem) {
			$_menus = $all[$key];
			if (isset ($menuItem['items']) && is_array($menuItem['items']) && count($menuItem['items'])) {
				$newMenus[$key]['name'] = $_menus['name'];
				$newMenus[$key]['items'] = $this->deMenu($menuItem['items'], $_menus['items']);
			} else {
				$newMenus[$key] = $_menus;
			}

		}
		return $newMenus;

	}

	function check($model, $action, $menus) {
		if (($model == 'admin') && (($action == 'index') || ($action=='login'))) {
			return true;
		}

		foreach ((array) $menus as $key => $menuItem) {
			if (isset ($menuItem['items']) && is_array($menuItem['items']) && count($menuItem['items'])) {
				$res = $this->check($model, $action, $menuItem['items']);
				if ($res) {
					return true;
				}
			}
			elseif (!empty ($menuItem['model']) && !empty ($menuItem['action']) && ($menuItem['model'] == $model) && ($menuItem['action'] == $action)) {
				return true;
			}

		}
		return false;
	}

	function getMenu($user) {
		$path = $this->getMenuPath($user);
		if (is_file($path)) {
			return include $path;
		} else {
			return array ();
		}
	}

	function getMenuPath($user) {
		return APP_PATH . '/users_menus/user_' . $user . '_menu.php';
	}

	function removeMenu($user) {
		$path = $this->getMenuPath($user);
		if (is_file($path)) {
			unlink($path);
			return true;
		} else {
			return false;
		}

	}

}
?>